//
//  AvatarCell.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/13.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit

class AvatarCell: UITableViewCell {
    
    var data: UserModel? {
        didSet {
            if let currentData = data {
                if let nickname = currentData.nickname {
                    nicknameLabel.text = nickname
                }
                if let username = currentData.username {
                    usernameLabel.text = "登录名：" + username
                }
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
        layoutUI()
    }
    
    fileprivate func prepareUI() {
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.contentView.addSubview(avatarImageView)
        self.contentView.addSubview(usernameLabel)
        self.contentView.addSubview(nicknameLabel)
    }
    
    fileprivate func layoutUI() {
        avatarImageView.snp.makeConstraints { (make) in
            make.top.equalTo(15)
            make.left.equalTo(10)
            make.centerY.equalToSuperview()
            make.width.equalTo(avatarImageView.snp.height)
        }
        
        nicknameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(avatarImageView.snp.top).offset(10)
            make.left.equalTo(avatarImageView.snp.right).offset(10)
        }
        
        usernameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nicknameLabel.snp.bottom).offset(10)
            make.left.equalTo(nicknameLabel.snp.left)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 创建 视图控件
    fileprivate lazy var avatarImageView: UIImageView = {
        let object = UIImageView()
        object.image = UIImage(named: "avatar.jpeg")
        object.layer.borderColor = UIColor.la_bg.cgColor
        object.layer.borderWidth = 0.5
        object.layer.cornerRadius = 2
        object.contentMode = .scaleAspectFit
        object.clipsToBounds = true
        return object
    }()
    
    fileprivate lazy var nicknameLabel: UILabel = {
        let object = UILabel()
        object.textAlignment = .left
        object.textColor = UIColor.black
        object.font = UIFont.la_Font(ofSize: 15)
        object.text = ""
        return object
    }()

    fileprivate lazy var usernameLabel: UILabel = {
        let object = UILabel()
        object.textAlignment = .left
        object.textColor = UIColor.la_gray
        object.font = UIFont.la_Font(ofSize: 12)
        object.text = "登录名："
        return object
    }()
}
